package com.seu.data.structures.segmentTree.leetcode2;

import com.seu.data.structures.segmentTree.basics.SegmentTree;

/// Leetcode 307. Range Sum Query - Mutable
/// https://leetcode.com/problems/range-sum-query-mutable/description/

/**
 * 计算动态数组指定区间元素之和
 *
 * @author liangfeihu
 * @since 2018/12/7 19:09
 */
class NumArray {

    private SegmentTree<Integer> segTree;

    public NumArray(int[] nums) {
        if (nums.length != 0) {
            Integer[] data = new Integer[nums.length];
            for (int i = 0; i < nums.length; i++) {
                data[i] = nums[i];
            }
            segTree = new SegmentTree<>(data, (a, b) -> a + b);
        }
    }

    public void update(int i, int val) {
        if (segTree == null) {
            throw new IllegalArgumentException("Error");
        }
        segTree.set(i, val);
    }

    public int sumRange(int i, int j) {
        if (segTree == null) {
            throw new IllegalArgumentException("Error");
        }
        return segTree.query(i, j);
    }

}
